package com.example.threadPoolExecutor;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MainTest {
    public static void main(String[] args) {
        //创建一个任务队列
        BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(5);
        //创建一个线程池
        ThreadPoolExecutor threadPoolExecutor =
                new ThreadPoolExecutor(5, 10, 0L, TimeUnit.MILLISECONDS, queue);
        //for循环创建任务
        for (int i = 0; i < 16; i ++) {
            try {
                threadPoolExecutor.execute(() -> {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程名称 : " + Thread.currentThread().getName());
                });
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        System.out.println("--------------------------------");
        System.out.println("CorePoolSize: "+threadPoolExecutor.getCorePoolSize());
        System.out.println("Queue: "+threadPoolExecutor.getQueue().size());
        System.out.println("MaximumPoolSize: "+threadPoolExecutor.getMaximumPoolSize());
        System.out.println("PoolSize: " + threadPoolExecutor.getPoolSize());
        System.out.println("--------------------------------");
        threadPoolExecutor.shutdown();
    }
}
